package com.quick.develop.flowable.config;

import com.quick.develop.flowable.framework.core.*;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType;
import org.flowable.common.engine.api.delegate.event.FlowableEventDispatcher;
import org.flowable.spring.SpringProcessEngineConfiguration;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;

import javax.annotation.Resource;

/**
 * @author junyuan.liu
 * @description:  全局监听配置 ContextRefreshedEvent在类被初始化之后触发
 * @date 2022/5/20 15:03
 */
@Configuration
public class FlowableGlobListenerConfig implements ApplicationListener<ContextRefreshedEvent> {
    @Resource
    private SpringProcessEngineConfiguration configuration;


    @Resource
    private BpmProcessStartedListener bpmProcessStartedListener;

    @Resource
    private BpmProcessCreatedListener bpmProcessCreatedListener;

    @Resource
    private BpmTaskCreatedListener bpmTaskCreatedListener;

    @Resource
    private BpmTaskAssignedListener bpmTaskAssignedListener;


    @Resource
    private BpmTaskCompletedListener bpmTaskCompletedListener;
    
    @Resource
    private BpmProcessCancelledListener bpmProcessCancelledListener;

    @Resource
    private BpmEntityDeletedListener bpmEntityDeletedListener;




    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        FlowableEventDispatcher dispatcher = configuration.getEventDispatcher();
        //挂载流程全局监听

        dispatcher.addEventListener(bpmProcessStartedListener,
                FlowableEngineEventType.PROCESS_STARTED);

        dispatcher.addEventListener(bpmProcessCreatedListener,
                FlowableEngineEventType.PROCESS_CREATED);

        dispatcher.addEventListener(bpmTaskAssignedListener,
                FlowableEngineEventType.TASK_ASSIGNED);

         dispatcher.addEventListener(bpmTaskCreatedListener,
                 FlowableEngineEventType.TASK_CREATED);

         dispatcher.addEventListener(bpmTaskCompletedListener, 
                 FlowableEngineEventType.TASK_COMPLETED);

         dispatcher.addEventListener(bpmProcessCancelledListener,
                 FlowableEngineEventType.PROCESS_CANCELLED);

         dispatcher.addEventListener(bpmEntityDeletedListener,
                 FlowableEngineEventType.ENTITY_DELETED);



    }
}
